/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mappers;

import handler.DatabaseConnection;
import java.io.FileNotFoundException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import model.Product;

/**
 *
 * @author Benjamin
 */
public class ProductMapper {
    
    private ProductMapper() {
    }
    
    public static ProductMapper getInstance() {
        return ProductMapperHolder.INSTANCE;
    }
    
    private static class ProductMapperHolder {

        private static final ProductMapper INSTANCE = new ProductMapper();
    }
    
    public Product getProduct(int id) throws SQLException, FileNotFoundException{
        
        Statement stmt = DatabaseConnection.getInstance().getConnection().createStatement();
        
        String sql = "select * from product where id = " + id;
        System.out.println(sql);
        
        ResultSet rs = stmt.executeQuery(sql);
        
        Product product = null;
        
        if(rs.next()){
            int pId = rs.getInt("id");
            String name = rs.getString("name");
            String description = rs.getString("description");
            int amount = rs.getInt("aktuelbeholdning");
            int minAmount = rs.getInt("minimumsbeholdning");
            
            product = new Product(id, name, description, amount, minAmount);
        }
        
        return product;
    }
}
